package com.itheima.service.impl;

import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
import com.itheima.pojo.PageBean;
import com.itheima.service.BrandService;
import com.itheima.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class BrandServiceImpl implements BrandService {
//1 创建sqlsessionfactory 对象
    SqlSessionFactory factory=SqlSessionFactoryUtils.getSqlSessionFactory();

    /**
     * 查询所有的=方法
     * @return
     */
    @Override
    public List<Brand> selectAll() {
        //2获取sqlsession对象
        SqlSession sqlSession = factory.openSession();
        //3获取brandmapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //4 调用方法
        List<Brand> brands = mapper.selectAll();
        //5释放资源
        sqlSession.close();
        return brands;
    }

    /**
     * 新增功能
     * @param brand
     */
    @Override
    public void add(Brand brand) {
        //2获取sqlsession对象
        SqlSession sqlSession = factory.openSession();
        //3获取brandmapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        mapper.add(brand);

        sqlSession.commit();

        sqlSession.close();
    }

    @Override
    public void deleteByIds(int[] ids) {
        //2获取sqlsession对象
        SqlSession sqlSession = factory.openSession();
        //3获取brandmapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        mapper.deleteByIds(ids);

        sqlSession.commit();
        sqlSession.close();
    }

    /**
     * 分页查询
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageBean<Brand> selectByPage(int currentPage, int pageSize) {
        //2获取sqlsession对象
        SqlSession sqlSession = factory.openSession();
        //3获取brandmapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //4 设置开始索引
       int begin= (currentPage-1)*pageSize;
       int size=pageSize;
       //5查询当页数据
        List<Brand> brands = mapper.selectByPage(begin, size);
        //6查询总数据量
        int TotalCount= mapper.selectTotalCount();
        //封装到javabean
        PageBean<Brand> brandPageBean = new PageBean<>();
        brandPageBean.setRows(brands);
        brandPageBean.setTotalCount(TotalCount);
        sqlSession.close();
        return brandPageBean;
    }

    /**
     * 分页条件查询
     * @param currentPage
     * @param pageSize
     * @param brand
     * @return
     */
    @Override
    public PageBean<Brand> selectByPageAndCondition(int currentPage, int pageSize, Brand brand) {
        //2获取sqlsession对象
        SqlSession sqlSession = factory.openSession();
        //3获取brandmapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        //4 设置开始索引
        int begin= (currentPage-1)*pageSize;
        int size=pageSize;
        //处理brand条件 模糊表达
        String brandName = brand.getBrandName();
        if(brandName!=null&&brandName.length()>0){
            brand.setBrandName("%"+brandName+"%");
        }
        String companyName = brand.getCompanyName();
        if(companyName!=null&&companyName.length()>0){
            brand.setCompanyName("%"+companyName+"%");
        }
        //5查询当页数据
        List<Brand> brands = mapper.selectByPageAndCondition(begin, size,brand);
        //6查询总数据量
        int TotalCount= mapper.selectTotalCountByCondition(brand);
        //封装到javabean
        PageBean<Brand> brandPageBean = new PageBean<>();
        brandPageBean.setRows(brands);
        brandPageBean.setTotalCount(TotalCount);
        sqlSession.close();
        return brandPageBean;

    }
}
